<?php
namespace app\admin\controller;

use think\Loader;
use think\Request;
use app\common\controller\Base;
use app\common\model\Anchor as AnchorModel;


/**
 * Class CaseCat
 * @package app\admin\controller
 */
class Anchor extends Base
{

    public function index(){
        $anchors = AnchorModel::order('id desc')->paginate(20);

        return view('',['anchors'=>$anchors]);
    }


    public function update(Request $request){
        if(!$request->isPost()){
            $this->error("来源渠道非法");
        }

        $data = $request->only('data');
        if(!$data){
            $this->success("操作成功");
        }
        $data = $data['data'];
        $newArr = [];
        foreach($data as $key => $val ){
            $tmp = [];
            $tmp['id'] = $key;
            foreach($val as $k => $v){
                $tmp[$k] = $v;
            }
            array_push($newArr,$tmp);
        }

        $anchor = new AnchorModel;
        $anchor->saveAll($newArr);
        $this->success("操作成功");
    }



    public function add(){

        return view();
    }

    public function create(Request $request){
        if(!$request->isPost()){
            $this->error("来源渠道非法");
        }

        $data = $request->only(['name','url']);

        $validate = Loader::validate("AnchorValidate");
        $result = $validate->scene('add')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }

        $anchor = new AnchorModel();
        $anchor ->save($data);
        $this->success("操作成功");
    }


    public function delete(Request $request){
        if(!$request->isDelete()){
            $this->error("来源渠道非法");
        }

        $id = $request -> param('id');
        AnchorModel::destroy($id);
        $this->success("删除成功");

    }


}